package com.gmail.fanvaron.Utils;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;

public class Utils {

    public static String INFINITE = "[Infinite]";

    public static String locationToString(Location location) {
        return location.getWorld().getName() + "#" + location.getX() + "#" + location.getY() + "#" + location.getZ();
    }

    public static Location stringToLocation(String s) {
        try {
            String[] split = s.split("#");
            World w = Bukkit.getServer().getWorld(split[0]);
            double x = Double.parseDouble(split[1]);
            double y = Double.parseDouble(split[2]);
            double z = Double.parseDouble(split[3]);
            return new Location(w, x, y, z);
            
            
        } catch (NumberFormatException e) {
            System.out.println("Fehler beim Parsen: "+ e.getMessage());
            return null;
        }

    }

    public static boolean hasMatchingSign(Block block, String text) {
        if (block.getRelative(BlockFace.EAST).getType() == Material.WALL_SIGN) {
            Sign sign = (Sign) block.getRelative(BlockFace.EAST).getState();
            if (sign.getLines()[0].equalsIgnoreCase(text)) {
                return true;
            }
        }
        if (block.getRelative(BlockFace.WEST).getType() == Material.WALL_SIGN) {
            Sign sign = (Sign) block.getRelative(BlockFace.WEST).getState();
            if (sign.getLines()[0].equalsIgnoreCase(text)) {
                return true;
            }
        }
        if (block.getRelative(BlockFace.NORTH).getType() == Material.WALL_SIGN) {
            Sign sign = (Sign) block.getRelative(BlockFace.NORTH).getState();
            if (sign.getLines()[0].equalsIgnoreCase(text)) {
                return true;
            }
        }
        if (block.getRelative(BlockFace.SOUTH).getType() == Material.WALL_SIGN) {
            Sign sign = (Sign) block.getRelative(BlockFace.SOUTH).getState();
            if (sign.getLines()[0].equalsIgnoreCase(text)) {
                return true;
            }
        }
        if (block.getRelative(BlockFace.UP).getType() == Material.WALL_SIGN) {
            Sign sign = (Sign) block.getRelative(BlockFace.UP).getState();
            if (sign.getLines()[0].equalsIgnoreCase(text)) {
                return true;
            }
        }
        return false;
    }
}
